//
//  BRSLoginViewController.m
//  Pint
//
//  Created by xiect on 13-10-24.
//  Copyright (c) 2013年 brains. All rights reserved.
//

#import "BRSLoginViewController.h"
#import "BRSMessageListViewController.h"

@interface BRSLoginViewController ()

@property(nonatomic,strong,readwrite)UITextField* tvUserName;
@property(nonatomic,strong,readwrite)UITextField* tvPassword;
@property(nonatomic,strong,readwrite)UIButton* btnLogin;
@property(nonatomic,strong,readwrite)UIButton* btnRegist;
@end

@implementation BRSLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)loadView{
    [super loadView];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1];
    UIImage * bgGroundImage = [UIImage imageNamed:@"login_background"];
    UIImageView * tempImageView = [[UIImageView alloc] initWithImage:bgGroundImage];
    [self.view addSubview:tempImageView];
    
    // 登录名Label
    int x ,y ,width,height;
    x = 20;
    y = 10;
    width = 290;
    height = 20;
    
    CGRect tempFrame = CGRectMake(x, y, width, height);
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:tempFrame];
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.text = @"ユーザーID";
    tempLabel.textAlignment = NSTextAlignmentLeft;
    tempLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:tempLabel];

    
    // 登录名 TEXT
    x = 20;
    y = y + height + 10;
    width = 280;
    height = 31;
    tempFrame = CGRectMake(x, y, width, height);
    UITextField * tempTxtView = [[UITextField alloc] initWithFrame:tempFrame];
    tempTxtView.backgroundColor = [UIColor clearColor];
    self.tvUserName = tempTxtView;
    [self.view addSubview:tempTxtView];
    
    
    // 密码Label
    x = 20;
    y = y + height;
    width = 290;
    height = 20;
    
    tempFrame = CGRectMake(x, y, width, height);
    tempLabel = [[UILabel alloc] initWithFrame:tempFrame];
    tempLabel.backgroundColor = [UIColor clearColor];
    tempLabel.text = @"パスワード";
    tempLabel.textAlignment = NSTextAlignmentLeft;
    tempLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:tempLabel];
    
    // 密码TEXT
    x = 20;
    y = y + height + 10;
    width = 280;
    height = 31;
    tempFrame = CGRectMake(x, y, width, height);
    tempTxtView = [[UITextField alloc] initWithFrame:tempFrame];
    tempTxtView.backgroundColor = [UIColor clearColor];
    self.tvPassword = tempTxtView;
    [self.view addSubview:tempTxtView];
    
    // 注册按钮
    x = 15;
    y = y + height + 20;
    width = 140;
    height = 41;
    
    tempFrame = CGRectMake(x, y, width, height);
    UIButton * tempButton = [[UIButton alloc] initWithFrame:tempFrame];
    UIImage *tempImage = [[UIImage imageNamed:@"login_signupbutton_background"] stretchableImageWithLeftCapWidth:7 topCapHeight:21];
    [tempButton setBackgroundImage:tempImage forState:UIControlStateNormal];
    [tempButton setTitle:@"新規" forState:UIControlStateNormal];
    
    self.btnRegist = tempButton;
    [self.view addSubview:tempButton];
    
    // 登录按钮
    x = x + width + 10;
    width = 140;
    height = 41;
    tempFrame = CGRectMake(x, y, width, height);
    tempButton = [[UIButton alloc] initWithFrame:tempFrame];
    tempImage = [[UIImage imageNamed:@"login_signinbutton_background"] stretchableImageWithLeftCapWidth:7 topCapHeight:21];
    [tempButton setBackgroundImage:tempImage forState:UIControlStateNormal];
    [tempButton setTitle:@"登録" forState:UIControlStateNormal];
    self.btnLogin = tempButton;
    [self.view addSubview:tempButton];
    
    [self.btnLogin addTarget:self action:@selector(doLogin:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnRegist addTarget:self action:@selector(doRegist:) forControlEvents:UIControlEventTouchUpInside];
}

-(void) doLogin:(id)sender{
    NSLog(@"========> doLogin------------");
    [SVProgressHUD show];
    [BRSApiClient sharedClient].parameterEncoding = XMLParameterEncoding;
    [[BRSApiClient sharedClient] loginWithId:@"" password:@"" onSuccess:^(NSMutableArray *array, NSError* error){
        NSLog(@"========> doLogin--------end %@",error);
        [SVProgressHUD dismiss];
        if(error){
            // 通信エラーを表示
            [[Utils getAppDelegate] alert:[error localizedDescription] message:[error localizedRecoverySuggestion]];
        }
    }];
}

-(void) doRegist:(id)sender{
    NSLog(@"========> doRegist------------");
    // メーユー画面を表示
    BRSMessageListViewController* controller = [[BRSMessageListViewController alloc] init];
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
